﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YmtAuth.AppService;
using YmtSystem.CrossCutting;
namespace YmtUserAuth.DataImport2
{
    public class DataImport
    {
        public static void Import()
        {
            try
            {
                var filePath = System.Configuration.ConfigurationManager.AppSettings["data"];
                var fullPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data" + "\\" + filePath);
                Console.WriteLine("执行导入数据");
                var watch = Stopwatch.StartNew();
                var fields = ReadFile.SyncRead(fullPath);
                var data = FieldToUserBills.Adapter(fields);
                Console.WriteLine("数据转换完毕 共 {0} 行， 耗时 {1} 秒 ", data.Count(), watch.Elapsed.TotalSeconds);
                Console.WriteLine("程序启动");
                YmatouFramework.Start();
                Console.WriteLine("程序启动完成，开始导入");
                var server = LocalServiceLocator.GetService<IUserBillsAppService>();
                var mongo = LocalServiceLocator.GetService<YmtAuth.Repository.Mongodb.MongoConext>();
                var page = data.Count() / 1000 + (data.Count() % 1000 > 0 ? 1 : 0);
                Console.WriteLine("page {0}", page);
                var db = System.Configuration.ConfigurationManager.AppSettings["db"] ?? "Ymt_Auth_201406";
                var tb = System.Configuration.ConfigurationManager.AppSettings["tb"] ?? "ULC_201406";
                Console.WriteLine("按任意键开始导入");
                Console.Read();
                for (var i = 0; i < page; i++)
                {
                    var datas = data.Skip(i * 1000).Take(1000);
                    ImportUser.BatchImport(datas, db, tb);
                }
                Console.WriteLine("完成导入 ，耗时 {0} 秒", watch.Elapsed.TotalSeconds);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.Read();
            YmatouFramework.Stop();
        }

        public static void Import2()
        {
            try
            {
                var filePath = System.Configuration.ConfigurationManager.AppSettings["data"];
                var fullPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data" + "\\" + filePath);
                Console.WriteLine("执行导入数据");
                var watch = Stopwatch.StartNew();
                var fields = ReadFile.SyncRead(fullPath);
                var data = FieldToUserBills.Adapter(fields).OrderBy(e => Convert.ToInt32(e["iUserId"]));
                Console.WriteLine("数据转换完毕 共 {0} 行， 耗时 {1} 秒 ", data.Count(), watch.Elapsed.TotalSeconds);
                Console.WriteLine("程序启动");
                YmatouFramework.Start();
                Console.WriteLine("程序启动完成，开始导入");
                var server = LocalServiceLocator.GetService<IUserBillsAppService>();
                var mongo = LocalServiceLocator.GetService<YmtAuth.Repository.Mongodb.MongoConext>();
                var db = System.Configuration.ConfigurationManager.AppSettings["db"] ?? "Ymt_Auth_201406";
                var tb = System.Configuration.ConfigurationManager.AppSettings["tb"] ?? "ULC_201406";
                int i = 0;
                data.Each(e =>
                {
                    try
                    {
                        //ImportUser.Import(e, db, tb);
                        ImportUser.ImportPassword(e);
                        System.Threading.Interlocked.Increment(ref i);
                        Console.WriteLine(i);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("1->{0}", ex.ToString());
                    }
                }, false, errorHandler: err => { Console.WriteLine("2->{0}", err.ToString()); });
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("3->{0}", ex.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine("4->{0}", ex.ToString());
            }
            Console.WriteLine("end");
            Console.Read();

        }

        public static void ImportThirdParty()
        {
            try
            {
                var filePath = System.Configuration.ConfigurationManager.AppSettings["data"];
                var fullPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data" + "\\" + filePath);
                Console.WriteLine("执行导入数据");
                var watch = Stopwatch.StartNew();
                var fields = ReadFile.SyncRead(fullPath);
                var data = FieldToUserBills.Adapter(fields).OrderBy(e => Convert.ToInt32(e["iUserId"]));
                Console.WriteLine("数据转换完毕 共 {0} 行， 耗时 {1} 秒 ", data.Count(), watch.Elapsed.TotalSeconds);
                Console.WriteLine("程序启动");
                YmatouFramework.Start();
                Console.WriteLine("程序启动完成，开始导入");
                var server = LocalServiceLocator.GetService<IUserBillsAppService>();
                var mongo = LocalServiceLocator.GetService<YmtAuth.Repository.Mongodb.MongoConext>();
                var db = System.Configuration.ConfigurationManager.AppSettings["db"] ?? "Ymt_Auth_201406";
                var tb = System.Configuration.ConfigurationManager.AppSettings["tb"] ?? "ULC_201406";
                int i = 0;
                data.Each(e =>
                {
                    //try
                    //{
                    ImportUser.ImportThirdParty(e, db, tb);
                    System.Threading.Interlocked.Increment(ref i);
                    Console.WriteLine(i);
                    //}
                    //catch (Exception ex)
                    //{
                    //    Console.WriteLine("1->{0}", ex.ToString());

                    //}
                }, false);
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("3->{0}", ex.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine("4->{0}", ex.ToString());
            }
            Console.WriteLine("end");
            Console.Read();

        }


        public static void BulkUpdateUserBills()
        {
            try
            {
                var db = System.Configuration.ConfigurationManager.AppSettings["db"] ?? "Ymt_Auth_201406";
                var tb = System.Configuration.ConfigurationManager.AppSettings["tb"] ?? "ULC_201406";
                var strPageCount = System.Configuration.ConfigurationManager.AppSettings["pageCount"] ?? "0";
                var pageCount = Convert.ToInt32(strPageCount);
                var strPageSize = System.Configuration.ConfigurationManager.AppSettings["pageSize"] ?? "0";
                var pageSize = Convert.ToInt32(strPageSize);


                YmatouFramework.Start();
                Console.WriteLine();
                Console.WriteLine("更新库 ：{0}，表 {1},pageCount {2},pageSize {3}", db, tb, pageCount, pageSize);
                Console.WriteLine("服务 ：{0}", System.Configuration.ConfigurationManager.AppSettings["mongourl"]);
                ImportUser.BulkUpdate(db, tb, pageSize, pageCount);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.Read();
        }

        public static void StatUserReg()
        {
            YmatouFramework.Start();
            ImportUser.ImportUserRegister();
        }
    }
}
